angular.module("detail")
    .controller("historyOrderController",function ($scope,$http) {

            $scope.cu=null;
            var req={
                method:"GET",
                url:"orders?taken={$ne:0}"
            }
            $http(req)
                .then(function (res) {
                    $scope.orders=res.data;
                    $scope.orders.forEach(function (item,index) {
                        if(item.taken==0){
                            item.taken="未接单";
                        }
                        if(item.taken==1){
                            item.taken="已接单";
                        }
                        if(item.taken==-2){
                            item.taken="不接单";
                        }
                        if(item.taken==-1){
                            item.taken="已收货";
                        }
                    })
                });

            $scope.processComments=function (index) {
                var req={
                    method:"GET",
                    url:"complaint?oid="+$scope.orders[index]._id
                }
                $http(req).then(function (res) {
                    if(!res.data.error){
                        console.log(res.data);
                        var but=document.getElementById($scope.orders[index]._id);
                        but.innerHTML=res.data.result;
                    }
                })
            }

    })
    .controller("orderManagerController",function ($scope,$http) {
            $scope.cu=null;

            var req1={
                method:"GET",
                url:"orders?taken=0"
            };
            $http(req1).then(function (res) {
                $scope.notTakenOrders=res.data;
                $scope.notTakenOrders=changeTaken($scope.notTakenOrders);
            });

            var req2={
                method:"GET",
                url:"orders?taken=1"
            };
            $http(req2).then(function (res) {
                $scope.takenOrders=res.data;
                $scope.takenOrders=changeTaken($scope.takenOrders);
            });

            var req3={
                method:"GET",
                url:"orders?taken=-2"
            };
            $http(req3).then(function (res) {
                $scope.doNotTakenOrders=res.data;
                $scope.doNotTakenOrders=changeTaken($scope.doNotTakenOrders);
            });
            $scope.processOrder=function (index,oid,taken) {
                var req4={
                    method:"PUT",
                    data:{taken:taken},
                    url:"order/"+oid
                }
                $http(req4).then(function (res) {
                    if(!res.data.error){
                        $scope.notTakenOrders.splice(index,1);
                        if(res.data.taken==1){
                            res.data.taken="已接单";
                            $scope.takenOrders.push(res.data);
                        }
                        if(res.data.taken==-2){
                            res.data.taken="不接单";
                            $scope.doNotTakenOrders.push(res.data);
                        }
                    }
                })
            }
            function changeTaken(data) {
            data.forEach(function (item,index) {
                    if(item.taken==0){
                        item.taken="未接单";
                    }
                    if(item.taken==1){
                        item.taken="已接单";
                    }
                    if(item.taken==-2){
                        item.taken="不接单";
                    }
                    if(item.taken==-1){
                        item.taken="已收货";
                    }
                })
                return data;
            }

    })
    .controller("merchantMsgController",['$scope','Upload','$timeout','$http',function($scope,Upload,$timeout,$http){
        $scope.cu={};
        $scope.img="选择图片";
        var req1={
            method:"GET",
            url:"merchant/1"
        };
        $http(req1)
            .then(function(res){
                $scope.m=res.data;
                $scope.cu=$scope.m;
            });
        $scope.uploadFiles=function(file,errFiles){
            $scope.f=file;
            $scope.cu.icon=$scope.f.name;
            $scope.errFiles=errFiles&&errFiles[0];
            if(file){
                file.upload=Upload.upload({
                    url:'uploads',
                    data:{file:file}
                });

                file.upload.then(function(response){
                    $timeout(function(){
                        $scope.img="上传成功";
                        file.result=response.data;
                    });
                },function(response){
                    if(response.status>0)
                        $scope.errorMsg=response.status+':'+response.data;


                },function(evt){
                    file.progress=Math.min(100,parseInt(100.0*evt.loaded/evt.total));
                });
            }
        }


        $scope.alert=function(){
            var req={
                method:"PUT",
                url:"merchant",
                data:$scope.cu,
                headers:{"Content-type":"application/json"}
            };

            $http(req)
                .then(function(res){
                    if(res.data.account){
                        $scope.cu=res.data;
                        $scope.m=$scope.cu;
                        $scope.result="修改成功";
                    }else{
                        $scope.result="修改失败";
                    }

                });
        }

    }])
    .controller("dishManagerController",function ($scope, $http) {
        var req = {
            method: "GET",
            url: "dishes"
        };
        $http(req)
            .then(function (res) {
                if (res.data.error){
                    window.location.href='merchantLogin1.html';
                }
                $scope.dishList = res.data;
                $scope.dishList.forEach(function (item) {
                    if (item.recommend == -1) {
                        $('#unBtn' + item._id).attr("disabled", true);
                        $('#recommendBtn' + item._id).attr("disabled", true);
                    } else if (item.recommend == 1) {
                        $('#recommendBtn' + item._id).attr("disabled", true);
                    }
                });
            });
        $scope.deleteDish = function (index) {
            var req = {
                method: "DELETE",
                url: "dish/" + $scope.dishList[index]._id
            };
            $http(req).then(function (res) {
                if (!res.data.error) {
                    $scope.dishList.splice(index, 1);
                }
            });
        }
        $scope.undercarriageDish = function (index) {
            var req = {
                method: "PUT",
                url: "dish/" + $scope.dishList[index]._id,
                data: {recommend: -1}
            };
            $http(req).then(function (res) {
                if (!res.data.error) {
                    $('#deleteBtn' + $scope.dishList[index]._id).attr("disabled", true);
                    $('#unBtn' + $scope.dishList[index]._id).attr("disabled", true);
                    $('#recommendBtn' + $scope.dishList[index]._id).attr("disabled", true);
                }
            });
        }
        $scope.recommendDish = function (index) {
            var req = {
                method: "PUT",
                url: "dish/" + $scope.dishList[index]._id,
                data: {recommend: 1}
            };
            $http(req).then(function (res) {
                if (!res.data.error) {
                    $('#deleteBtn' + $scope.dishList[index]._id).attr("disabled", true);
                    $('#recommendBtn' + $scope.dishList[index]._id).attr("disabled", true);
                }
            });
        }
    })
    